﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace VH.SharePoint.Api.WLW
{
    //Ref: http://msdn.microsoft.com/en-us/library/bb463265.aspx
    [XmlRoot("manifest", Namespace = "http://schemas.microsoft.com/wlw/manifest/weblog", IsNullable = true)]
    public class Manifest
    {
        [XmlElement("options")]
        public Supports Options { get; set; }

        [XmlElement("weblog")]
        public WebLog WebLog { get; set; }

        [XmlArray("buttons")]
        [XmlArrayItem(typeof(Button),ElementName = "button")]
        public List<Button> Buttons { get; set; }

        [XmlElement("views")]
        //[XmlArrayItem(typeof(Button), ElementName = "button")]
        public Views Views { get; set; }

        public Manifest()
        {
            
        }

        public override string ToString()
        {
            return ToXml();
        }
        public string ToXml()
        {
            var xmls = new XmlSerializer(this.GetType());
            using (var ms = new MemoryStream())
            {
                var settings = new XmlWriterSettings();
                settings.Encoding = Encoding.UTF8;
                settings.Indent = true;
                settings.IndentChars = "\t";
                settings.NewLineChars = Environment.NewLine;
                settings.ConformanceLevel = ConformanceLevel.Document;
                
                var ns = new XmlSerializerNamespaces();
                ns.Add("", "http://schemas.microsoft.com/wlw/manifest/weblog");
                
                using (var writer = XmlTextWriter.Create(ms, settings))
                {
                    xmls.Serialize(writer, this, ns);
                }

                var xml = Encoding.UTF8.GetString(ms.ToArray());
                return xml;
            }
        }
    }
}
